package com.emm.yixun.mobile.adapter;

import java.util.ArrayList;

import android.app.Activity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.emm.yixun.mobile.R;
import com.emm.yixun.mobile.application.EmmApplication;
import com.emm.yixun.mobile.model.GetDictionaryList.RqBusNumList;

public class FoucsAdapter extends BaseAdapter {

	ArrayList<RqBusNumList> RqFocus;

	Activity c;
	int type = 0;

	public FoucsAdapter(Activity c, ArrayList<RqBusNumList> RqFocus) {
		this.RqFocus = RqFocus;
		this.c = c;
	}

	public void GetDate(ArrayList<RqBusNumList> RqFocus) {
		this.RqFocus = RqFocus;
		notifyDataSetChanged();
	}

	public void SetType(int type) {
		this.type = type;
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return RqFocus == null ? 0 : RqFocus.size();
	}

	public ArrayList<RqBusNumList> getList() {
		return RqFocus;
	}

	@Override
	public Object getItem(int position) {
		return RqFocus == null ? null : RqFocus.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHd v = null;
		if (convertView == null) {
			convertView = LayoutInflater.from(c).inflate(R.layout.focus_item, null);
			v = new ViewHd();
			v.spinner_name = (TextView) convertView.findViewById(R.id.spinner_name);
			v.select_image_att = (ImageView) convertView.findViewById(R.id.select_image_att);
			v.imageView1_delete = (ImageView) convertView.findViewById(R.id.imageView1_delete);
			v.count_textview = (TextView) convertView.findViewById(R.id.count_textview);
			v.focus_item_lines = convertView.findViewById(R.id.focus_item_lines);
			convertView.setTag(v);
		} else {
			v = (ViewHd) convertView.getTag();
		}
		v.spinner_name.setText(RqFocus.get(position).getDictionaryName());

		if ((RqFocus.size() - 1) == position) {
			v.focus_item_lines.setVisibility(View.VISIBLE);
		} else {
			v.focus_item_lines.setVisibility(View.GONE);
		}

		if (type != 0) {
			v.select_image_att.setVisibility(View.INVISIBLE);
			v.count_textview.setVisibility(View.VISIBLE);
			if (type == 9) {
				v.imageView1_delete.setVisibility(View.GONE);
				v.focus_item_lines.setVisibility(View.GONE);
			} else {
				v.imageView1_delete.setVisibility(View.VISIBLE);
			}
		} else {
			v.count_textview.setVisibility(View.GONE);
			v.select_image_att.setVisibility(View.VISIBLE);
			v.imageView1_delete.setVisibility(View.GONE);
		}
		v.imageView1_delete.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				for (int p = 0; p < EmmApplication.RqFocus.size(); p++) {
					if (EmmApplication.RqFocus.get(p).getDictionaryId().equals(RqFocus.get(position).getDictionaryId())) {
						EmmApplication.RqFocus.get(p).setSelect(false);
					}
				}
				RqFocus.remove(position);
				notifyDataSetChanged();
			}
		});
		if (RqFocus.get(position).isSelect()) {
			v.select_image_att.setBackgroundResource(R.drawable.ico_selt);
		} else {
			v.select_image_att.setBackgroundResource(R.drawable.ico_delt_no);
		}
		convertView.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Log.w("p", "" + position);
				if (RqFocus.get(position).isSelect()) {
					RqFocus.get(position).setSelect(false);
				} else {
					RqFocus.get(position).setSelect(true);
				}
				notifyDataSetChanged();
			}
		});
		return convertView;
	}

	public static class ViewHd {
		TextView spinner_name, count_textview;

		ImageView select_image_att, imageView1_delete;

		View focus_item_lines;
	}
}
